using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Petra.Gis;
using Petra.Gis.Geometry;
using Petra.Gis.Geometry.Support;
using Petra.Gis.DataServices.WKT.Support;
using System.IO;

namespace Petra.Gis.DataServices.WKT
{
  public partial class GeometryFactory
  {
    /***********************************************
     * 28.8.2007
     * 
     * creates MultiLine
     * from wkt string
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/
    /// <summary>
    /// MultiLineToString takes LineString only geometry collection as input
    /// and converts all elements in collection from startIndex to binary
    /// startIndex is first element to process
    /// </summary>
    private static GeometryBase ToMultiLine(ref string inputStr, System.Type parentGeomType)
    {

      RegexGeometryParser rgp = new RegexGeometryParser(WKTGeometryType.MultiLineString);
      RegexGeometry rg = rgp.ParseGeometry(ref inputStr);

      MultiLine oGeom = new MultiLine();

      foreach (RegexGeometry tmpRegexGeom in rg)
      {

        LineString tmpGeom = ToLineString(tmpRegexGeom, WKTGeometryType.MultiLineString);
        oGeom.Add(tmpGeom);
      }

      return oGeom;


      /*
      //prepare regex expr.
      string pattern = @"(?<whitespace>\s*)" +
        @"(?<geometryname>" + getWktName[WKTGeometryType.MultiLineString] + ")" +
        @"(?<multilineopen>\s*\()" +
        @"((?<linestringopen>\s*\()" +
        @"(?<coordinates>(\s*|\s*\,{1}\s*|\s+)\-?(\d*\.?\d+|\d+\.?\d*)([eE][+-]?\d+)?)+" +
        @"(?<linestringclose>\s*\)))+" +
        @"(?<multilineclose>\s*\))"; // +
      //@"(?<invalid>[^\s]+)"; //put all regex exps here to avoid invalid chars in input string

      Match m;

      Regex r = new Regex(pattern);

      m = r.Match(inputStr);
      if (m.Length < 1)
        throw new ArgumentException("invalid input string for wkt", "inputStr");

      //validate capture ring
      if (m.Groups["linestringopen"].Captures.Count != m.Groups["linestringclose"].Captures.Count)
        throw new ArgumentException("invalid input string for wkt", "inputStr");

      MultiLine oGeom = new MultiLine();


      //CoordinateList coords;
      //int beginIndex = 0;
      LineString tmpLR;

      for (int i = 0; i < m.Groups["linestringclose"].Captures.Count; i++)
      {
        tmpLR = ToLineString(m.Groups["coordinates"].Captures, m.Groups["linestringopen"].Captures[i], m.Groups["linestringclose"].Captures[i], oGeom.GetType());
        oGeom.Add(tmpLR);
      }

      return oGeom;
      */
    }







    /***********************************************
     * 28.8.2007
     * 
     * creates wkt string 
     * from MultiLine
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/

    /// <summary>
    /// MultiLineToString takes Line only geometry collection as input
    /// and converts all elements in collection from startIndex to string
    /// startIndex is first element to process
    /// </summary>
    private static string MultiLineToString(GeometryBase geometry)
    {
      MultiLine _mg = (MultiLine)geometry;

      System.IO.StringWriter mStrWriter = new System.IO.StringWriter();

      mStrWriter.Write(getWktName[WKTGeometryType.MultiLineString]);
      mStrWriter.Write("(");

      LineString tmpLine;
      string sep = "";
      foreach (GeometryBase g in _mg)
      {
        //cast GeometryBase to LineRing
        tmpLine = (LineString)g;
        mStrWriter.Write("(");
        mStrWriter.Write(sep);
        mStrWriter.Write(CoordinateListConverter.ToString(tmpLine.coordList, Dimension.dim2D));
        mStrWriter.Write(")");
        sep = ",";
      }
      mStrWriter.Write(")");

      return mStrWriter.ToString();

    }

  }
}
